package org.gw.building.ctrl;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.gw.building.dto.Tyxx;
import org.gw.building.model.Bm;
import org.gw.building.model.Tyxxb;
import org.gw.building.model.Tyxxcjb;
import org.gw.building.model.Tyxxcjsz;
import org.gw.building.model.Xxcjshzt;
import org.gw.building.model.base.Gxzt;
import org.gw.building.model.base.Mz;
import org.gw.building.model.base.Rylx;
import org.gw.building.model.base.Sjly;
import org.gw.building.model.base.Tyxxcjzb;
import org.gw.building.model.base.Xb;
import org.gw.building.model.base.Zzmm;
import org.gw.building.repositories.TyxxbRepository;
import org.gw.building.repositories.TyxxcjbRepository;
import org.gw.building.repositories.TyxxcjszRepository;
import org.gw.building.repositories.TyxxcjzbRepository;
import org.gw.ylc.base.context.RequestContextHolder;
import org.gw.ylc.base.interceptor.RequestPermission;
import org.gw.ylc.base.model.Department;
import org.gw.ylc.base.model.Student;
import org.gw.ylc.base.model.UserInfo;
import org.gw.ylc.base.repositories.StudentRepository;
import org.gw.ylc.base.util.PermissionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by qwx89 on 2017/8/6.
 */
@RestController
@RequestMapping("tyxxcj")
public class TyxxcjCtrl {

  @Autowired
  private TyxxcjzbRepository tyxxcjzbRepository;

  @Autowired
  private TyxxcjszRepository tyxxcjszRepository;

  @Autowired
  private TyxxbRepository tyxxbRepository;

  @Autowired
  private TyxxcjbRepository tyxxcjbRepository;

  @Autowired
  private StudentRepository studentRepository;

  @RequestMapping("list")
  @RequestPermission({RequestPermission.BRANCH})
  public Page<Tyxxcjzb> list() {
    UserInfo userInfo = RequestContextHolder.get().getUser();
    String departmentId = userInfo.getDepartment();
    Page<Tyxxcjzb> cjzbs = tyxxcjzbRepository.queryYxCjzb(departmentId, RequestContextHolder.get().getPageable());
    return cjzbs;
  }

  @RequestMapping("listTyxx")
  @RequestPermission({RequestPermission.BRANCH})
  @Transactional
  public Object listTyxx(String xh, String cjzb) {
    Page<Tyxxcjb> cjbs = null;
    if (StringUtils.isNotBlank(cjzb)) {
      cjbs = tyxxcjbRepository.findTyxx(PermissionUtil.toLikeString(xh), cjzb, RequestContextHolder.get().getPageable());
    }
    if (cjbs == null || cjbs.getSize() == 0) {
      UserInfo userInfo = RequestContextHolder.get().getUser();
      String zbsjXh = userInfo.getUid();//支部书记学号
      Tyxxb tyxx = tyxxbRepository.findZbsjByXh(zbsjXh);
      Bm zb = tyxx.getZbmc();
      List<Tyxxb> tyxxbs = tyxxbRepository.findZBTyxx(zb);
      if (CollectionUtils.isEmpty(tyxxbs)) {
        return -1;
      }
      Tyxxcjzb tyxxcjzb = getCjzb(cjzb, userInfo.getId(), userInfo.getName(), userInfo.getDepartment(), zb);
      List<Tyxxcjb> copyCjbs = copyTyxxbs(tyxxbs, tyxxcjzb);

      tyxxcjbRepository.save(copyCjbs);
      cjbs = tyxxcjbRepository.findTyxx(PermissionUtil.toLikeString(xh), tyxxcjzb.getId(), RequestContextHolder.get().getPageable());
    }
    return cjbs;
  }

  private List<Tyxxcjb> copyTyxxbs(List<Tyxxb> tyxxbs, Tyxxcjzb tyxxcjzb) {
    List<Tyxxcjb> copyCjbs = new ArrayList<>();
    for (Tyxxb s : tyxxbs) {
      Tyxxcjb cjb = copyTyxxb(s, tyxxcjzb);
      copyCjbs.add(cjb);
    }
    return copyCjbs;
  }

  private Tyxxcjb copyTyxxb(Tyxxb s, Tyxxcjzb tyxxcjzb) {
    Tyxxcjb cjb = new Tyxxcjb();
    BeanUtils.copyProperties(s, cjb);
    cjb.setCjzbid(tyxxcjzb.getId());
    Gxzt gxzt = new Gxzt();
    gxzt.setDm("4");
    cjb.setGxzt(gxzt);
    return cjb;
  }

  @Transactional
  Tyxxcjzb getCjzb(String cjzb, String id, String userName, String departmentId, Bm zb) {
    Tyxxcjzb tyxxcjzb = null;
    if (StringUtils.isNotBlank(cjzb)) {
      tyxxcjzb = tyxxcjzbRepository.findOne(cjzb);
    }
    if (tyxxcjzb == null) {
      tyxxcjzb = new Tyxxcjzb();
      tyxxcjzb.setSbrid(id);
      tyxxcjzb.setSbr(userName);
      tyxxcjzb.setZbmc(zb);
      Department department = new Department();
      department.setId(departmentId);
      tyxxcjzb.setYx(department);
      Xxcjshzt xxcjshzt = new Xxcjshzt();
      xxcjshzt.setDm("10");
      tyxxcjzb.setShzt(xxcjshzt);
      tyxxcjzb = tyxxcjzbRepository.save(tyxxcjzb);
    }
    return tyxxcjzb;
  }

  @RequestMapping("cjsz")
  @RequestPermission({RequestPermission.BRANCH})
  public Tyxxcjsz getCjsz() {
    Iterable<Tyxxcjsz> cjszs = tyxxcjszRepository.findAll();
    Iterator<Tyxxcjsz> it;
    if (cjszs != null && (it = cjszs.iterator()) != null && it.hasNext()) {
      return it.next();
    }
    return null;
  }

  @RequestMapping("getAuditNum")
  @RequestPermission({RequestPermission.BRANCH})
  public int getAuditNum() {
    UserInfo userInfo = RequestContextHolder.get().getUser();
    String departmentId = userInfo.getDepartment();
    return tyxxcjzbRepository.getAuditNum(departmentId);
  }

  @RequestMapping("chooseClass")
  @RequestPermission({RequestPermission.BRANCH})
  @Transactional
  public Object chooseClass(String bjmc, String cjzb) {
    if (StringUtils.isBlank(bjmc)) {
      throw new RuntimeException("班级名称不能为空");
    }
    List<Student> students = studentRepository.findByClassName(bjmc);
    if (CollectionUtils.isEmpty(students)) {
      return false;
    }
    final List<Tyxxcjb> cjbs = new ArrayList<>();
    UserInfo userInfo = RequestContextHolder.get().getUser();
    String departmentId = userInfo.getDepartment();
    String zbsjXh = userInfo.getUid();//支部书记学号
    Tyxxb tyxx = tyxxbRepository.findZbsjByXh(zbsjXh);
    Bm bm = tyxx.getZbmc();
    Tyxxcjzb tyxxcjzb = getCjzb(cjzb, userInfo.getId(), userInfo.getName(), departmentId, bm);
    final Gxzt gxzt = new Gxzt();
    gxzt.setDm("4");
    students.forEach(stu -> {
      if (StringUtils.equals(userInfo.getUid(), stu.getXh())) {
        return;
      }
      Tyxxcjb cjb = Tyxxcjb.builder().cjzbid(tyxxcjzb.getId()).id(stu.getId()).
              yx(departmentId).zbmc(bm).xh(stu.getXh()).xm(stu.getName()).
              xb(stu.getSex() == 0 ? new Xb("1") : new Xb("2")).mz(new Mz(stu.getEthnicGroup().getCode())).
              csrq(stu.getBirthdate()).zzmm(new Zzmm(stu.getPolicital().getCode())).
              whcd(stu.getWhcd()).tgb(false).zbsj(false).zbwy(false).czr(userInfo.getName()).czsj(new Date()).
              lx(new Rylx("1")).sjly(new Sjly("1")).scbz(false).gxzt(gxzt).build();
      cjbs.add(cjb);
    });
    tyxxcjbRepository.save(cjbs);
    return tyxxcjzb.getId();
  }

  @RequestMapping("deleteTyxx")
  @RequestPermission({RequestPermission.BRANCH,RequestPermission.DEPARTMENT, RequestPermission.SCHOOL})
  public void deleteTyxx(String cjbid) {
    Assert.hasText(cjbid, "无效的团员信息");
    Tyxxcjb tyxxcjb = tyxxcjbRepository.findOne(cjbid);
    if (tyxxcjb.realDel()) {
      tyxxcjbRepository.delete(tyxxcjb);
    } else {
      Gxzt gxzt = new Gxzt();
      gxzt.setDm("3");
      tyxxcjb.setGxzt(gxzt);
      tyxxcjbRepository.save(tyxxcjb);
    }
  }

  @RequestMapping("stu")
  @RequestPermission({RequestPermission.BRANCH})
  public Student getStu(String xh) {
    return studentRepository.findByXh(xh);
  }

  @RequestMapping("addTyxx")
  @RequestPermission({RequestPermission.BRANCH,RequestPermission.DEPARTMENT, RequestPermission.SCHOOL})
  public Object addTyxx(Tyxxcjb cjb) {
    UserInfo userInfo = RequestContextHolder.get().getUser();
    if (StringUtils.isNotBlank(cjb.getXh())) {
      Gxzt gxzt = new Gxzt();
      gxzt.setDm("2");
      cjb.setGxzt(gxzt);
      cjb.setXgr(userInfo.getName());
      cjb.setXgsj(new Date());
      return tyxxcjbRepository.save(cjb);
    }
    Tyxx tyxx = cjb.getTyxx();
    Assert.notNull(tyxx, "无效的学生");
    String xh = tyxx.getXh();
    Student stu = studentRepository.findByXh(xh);

    String departmentId = userInfo.getDepartment();
    String zbsjXh = userInfo.getUid();//支部书记学号
    Tyxxb tyxxb = tyxxbRepository.findZbsjByXh(zbsjXh);
    Bm bm = tyxxb.getZbmc();
    cjb.setId(stu.getId());
    cjb.setYx(departmentId);
    cjb.setZbmc(bm);
    cjb.setXh(stu.getXh());
    cjb.setXm(stu.getName());
    cjb.setXb(stu.getSex() == 0 ? new Xb("1") : new Xb("2"));
    cjb.setCsrq(stu.getBirthdate());
    cjb.setMz(new Mz(stu.getEthnicGroup().getCode()));
    cjb.setZzmm(new Zzmm(stu.getPolicital().getCode()));
    cjb.setWhcd(stu.getWhcd());
    cjb.setLx(new Rylx("1"));
    cjb.setScbz(false);
    cjb.setSjly(new Sjly("1"));
    cjb.setCzr(userInfo.getName());
    cjb.setCzsj(new Date());
    Gxzt gxzt = new Gxzt();
    gxzt.setDm("1");
    cjb.setGxzt(gxzt);
    return tyxxcjbRepository.save(cjb);
  }

  @RequestMapping("submit")
  @RequestPermission({RequestPermission.BRANCH})
  public Object submit(String cjzbid) {
    Assert.hasText(cjzbid, "采集主表不能为空");
    Tyxxcjzb tyxxcjzb = tyxxcjzbRepository.findOne(cjzbid);
    tyxxcjzb.setShzt(new Xxcjshzt("20"));
    return tyxxcjzbRepository.save(tyxxcjzb);
  }

  @RequestMapping("delTyxxcjzb")
  @RequestPermission({RequestPermission.BRANCH})
  @Transactional
  public void delTyxxcjzb(Tyxxcjzb cjzb) {
    Assert.notNull(cjzb, "无效的采集主表信息");
    tyxxcjbRepository.delTyxxByCjzb(cjzb.getId());
    tyxxcjzbRepository.delete(cjzb);
  }
}
